﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 护士工作站
{
    public partial class 出院病人费用查询 : Form
    {
        public 出院病人费用查询()
        {
            InitializeComponent();
        }
        DataTable tb = null;
        SqlDataAdapter da = null;
        DataSet ds = null;
        private void DataBind(string sql)
        {
            ds = new DataSet();
            SqlConnection sqlConnection = new SqlConnection();
            sqlConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = sql;
            da = new SqlDataAdapter(sqlCommand);
            SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            sqlConnection.Open();
            da.Fill(ds);
            sqlConnection.Close();
            tb = ds.Tables[0];
            dgvxinxi.DataSource = tb;
        }
    
        public void Fill()
        {



            string sql = string.Format("select * from  [hushigongzuozhan1].[dbo].[feiyong] where  HosNo = '{0}'", txtzhuyuanhao.Text.Trim());


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dgvxinxi.DataSource = ds.Tables[0];
            } 
        }

 public void Fill1()
        {
            string sql = string.Format("select * from [hushigongzuozhan1].[dbo].[feiyong] where selypay = '0'  and HosNo = '{0}'", txtzhuyuanhao.Text.Trim());
            DataBind(sql);

        }
        public void Fill2()
        {
            string sql = string.Format("select * from  [hushigongzuozhan1].[dbo].[feiyong] where selypay = '1'  and HosNo = '{0}'", txtzhuyuanhao.Text.Trim());
            DataBind(sql);

        }


       

        private void btntiqv_Click(object sender, EventArgs e)
        {
            string connString = DBHelper.connString;
            string sqlstr = string.Format("select * from InHospital,Patient,Warn  where   InHospital.HosNo =  Patient.HosNo  and  Patient.HosNo = Warn.HosNo   and [hushigongzuozhan1].[dbo].[Patient].Condition= '出院' and InHospital.HosNo= '{0}'", txtzhuyuanhao.Text.Trim());

            using (SqlConnection conn = new SqlConnection(connString))
            {
                SqlCommand cmd = new SqlCommand(sqlstr, conn);

                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    txtsdate.Text = dr["WarBegindate"].ToString();
                    txtfdate.Text = dr["WarFinishdate"].ToString();
                    txtname.Text = dr["PatName"].ToString();
                    txtxingbie.Text = dr["PatSex"].ToString();
                    txtbed.Text = dr["BedNo"].ToString();
                    txtruyuandate.Text = dr["HosIndate"].ToString();
                    txtjiecuandate.Text = dr["HosOfficeDate"].ToString();
                    txtyujiaojin.Text = dr["HosPrepay"].ToString();
                    txtfenlei.Text = dr["PatType"].ToString();


                }
                dr.Close();
            }
        }

        private void btnjizhang_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select * from [hushigongzuozhan1].[dbo].[feiyong] where selypay = '0'  and HosNo = '{0}'", txtzhuyuanhao.Text.Trim());
            Fill1();
        }

        private void btnyujiaojin_Click(object sender, EventArgs e)
        {
            Fill2();
        }

        private void rbtbuhebing_CheckedChanged(object sender, EventArgs e)
        {
            Fill1();
        }

        private void rbthebing_CheckedChanged(object sender, EventArgs e)
        {

            Fill();
        }

        private void btnqingping_Click(object sender, EventArgs e)
        {
            txtzhuyuanhao.Text = "";
            txtfenlei.Text = "";
            txtsdate.Text = "";
            txtfdate.Text = "";
            txtname.Text = "";
            txtxingbie.Text = "";
            txtbed.Text = "";
            txtruyuandate.Text = "";
            txtjiecuandate.Text = "";
            txtyujiaojin.Text = "";
            Fill();
        }

        private void button_sc_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                id = (int)dgvxinxi.CurrentRow.Cells[6].Value;

            }
            catch (System.Exception )
            {
                MessageBox.Show("请选择有效数据行");
                return;
            }
            if (MessageBox.Show("确定要删除吗", " 询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                return;

            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string sql = string.Format("delete from feiyong where xiangmuNo = {0}", id);
                SqlCommand cmd = new SqlCommand(sql, conn);
                conn.Open();
                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                if (n != 1)
                {
                    MessageBox.Show("删除失败");
                }
                else
                {
                    MessageBox.Show("删除成功");
                }

                Fill();
                Fill1();
                Fill2();

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder scb = new SqlCommandBuilder(da);
            try
            {
                da.Update(ds);

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
